package com.ruoyi.ws.facade;


import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.system.domain.SysUser;
import com.ruoyi.system.domain.UserClass;
import com.ruoyi.system.responseDto.StudentResponseDto;
import com.ruoyi.system.responseDto.UserDto;
import com.ruoyi.system.service.IAdminClassService;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.system.service.IUserClassService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import static com.ruoyi.common.core.domain.AjaxResult.error;
import static com.ruoyi.common.core.domain.AjaxResult.success;

/**
 * 路径规划显示配置规则REST服务：提供有关路径规划显示配置规则的接口
 *
 * @author roytrack
 * @date 2020-05-25 17:37:24
 */
@RestController
@RequestMapping(path = "services/class")
public class ClassResource {

  /**
   * LOGGER
   */
  private static final Logger LOGGER = LoggerFactory.getLogger(ClassResource.class);
  @Autowired
  private IUserClassService userClassService;
  @Autowired
  private ISysUserService userService;
  @Autowired
  private IAdminClassService adminClassService;

  @PostMapping("/getStudentList")
  @ResponseBody
  public AjaxResult getStudentList (Long classId) {
    List<UserDto> sysUserList = new ArrayList<UserDto>();
    try {
      //学生班级关系表
      UserClass userClass = new UserClass();
      userClass.setClassId(classId);
      List<UserClass> userClassList = userClassService.selectUserClassList(userClass);
      for (UserClass userClass1 : userClassList) {
        SysUser sysUser = userService.selectUserById(userClass1.getUserId());
        if(null!=sysUser){
          UserDto userDto = new UserDto();
          BeanUtils.copyProperties(sysUser,userDto);
          sysUserList.add(userDto);
        }
      }
      //总数
      int totalCount =0;
      //在线数
      int onlintCount =0;
      totalCount = sysUserList.size();
      List<UserDto> sysUserList1 = sysUserList.stream().filter(item -> "1".equals(item.getLoginFlag())).collect(Collectors.toList());
      onlintCount = sysUserList1==null?0:sysUserList1.size();
      StudentResponseDto studentResponseDto = new StudentResponseDto();
      studentResponseDto.setOnlineCount(onlintCount);
      studentResponseDto.setTotalCount(totalCount);
      studentResponseDto.setSysUserList(sysUserList);

      return success(studentResponseDto);
    } catch (Exception e) {
      LOGGER.info("获取班级下对学生失败",e);
      return error("获取班级下对学生失败");
    }
  }

}
